Imports Phoenix.DataAccess
Imports Phoenix.Events

Namespace Forms
    Public Class ServiceForm : Inherits ChildFormBase

#Region " Windows Form Designer generated code "

        Public Sub New()
            MyBase.New()

            'This call is required by the Windows Form Designer.
            InitializeComponent()

            'Add any initialization after the InitializeComponent() call
            MasterLevelIDGridColumn.DisplayFormat.FormatType = DevExpress.Utils.FormatType.Custom
            MasterLevelIDGridColumn.DisplayFormat.Format = New MasterLevelFormatter
        End Sub

        'UserControl overrides dispose to clean up the component list.
        Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
            If disposing Then
                If Not (components Is Nothing) Then
                    components.Dispose()
                End If
            End If
            MyBase.Dispose(disposing)
        End Sub

        'Required by the Windows Form Designer
        Private components As System.ComponentModel.IContainer

        'NOTE: The following procedure is required by the Windows Form Designer
        'It can be modified using the Windows Form Designer.  
        'Do not modify it using the code editor.
        Friend WithEvents lblLabel As System.Windows.Forms.Label
        Friend WithEvents lblType As System.Windows.Forms.Label
        Friend WithEvents LabelBox As DevExpress.XtraEditors.TextEdit
        Friend WithEvents TypeCombo As DevExpress.XtraEditors.ComboBoxEdit
        Friend WithEvents ServiceParamsGridControl As DevExpress.XtraGrid.GridControl
        Friend WithEvents ServiceParamsGridView As DevExpress.XtraGrid.Views.Grid.GridView
        Friend WithEvents GridColumnPrice As DevExpress.XtraGrid.Columns.GridColumn
        Friend WithEvents GridColumnDuration As DevExpress.XtraGrid.Columns.GridColumn
        Friend WithEvents CommentMemoEdit As DevExpress.XtraEditors.MemoEdit
        Friend WithEvents MasterLevelIDGridColumn As DevExpress.XtraGrid.Columns.GridColumn
        Friend WithEvents Label1 As System.Windows.Forms.Label
        Friend WithEvents DefaultColumn As DevExpress.XtraGrid.Columns.GridColumn
        Friend WithEvents PriceRI As DevExpress.XtraEditors.Repository.RepositoryItemCalcEdit
        Friend WithEvents DurationRI As DevExpress.XtraEditors.Repository.RepositoryItemSpinEdit
        Friend WithEvents DefaultRI As DevExpress.XtraEditors.Repository.RepositoryItemButtonEdit
        Friend WithEvents lblColor As System.Windows.Forms.Label
        Friend WithEvents ColorBox As DevExpress.XtraEditors.ColorEdit
        Friend WithEvents TextColorBox As DevExpress.XtraEditors.ColorEdit
        Friend WithEvents lblTextColor As System.Windows.Forms.Label
        <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
            Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(ServiceForm))
            Me.LabelBox = New DevExpress.XtraEditors.TextEdit
            Me.lblLabel = New System.Windows.Forms.Label
            Me.lblType = New System.Windows.Forms.Label
            Me.TypeCombo = New DevExpress.XtraEditors.ComboBoxEdit
            Me.ServiceParamsGridControl = New DevExpress.XtraGrid.GridControl
            Me.ServiceParamsGridView = New DevExpress.XtraGrid.Views.Grid.GridView
            Me.MasterLevelIDGridColumn = New DevExpress.XtraGrid.Columns.GridColumn
            Me.GridColumnPrice = New DevExpress.XtraGrid.Columns.GridColumn
            Me.PriceRI = New DevExpress.XtraEditors.Repository.RepositoryItemCalcEdit
            Me.GridColumnDuration = New DevExpress.XtraGrid.Columns.GridColumn
            Me.DurationRI = New DevExpress.XtraEditors.Repository.RepositoryItemSpinEdit
            Me.DefaultColumn = New DevExpress.XtraGrid.Columns.GridColumn
            Me.DefaultRI = New DevExpress.XtraEditors.Repository.RepositoryItemButtonEdit
            Me.CommentMemoEdit = New DevExpress.XtraEditors.MemoEdit
            Me.Label1 = New System.Windows.Forms.Label
            Me.lblColor = New System.Windows.Forms.Label
            Me.ColorBox = New DevExpress.XtraEditors.ColorEdit
            Me.TextColorBox = New DevExpress.XtraEditors.ColorEdit
            Me.lblTextColor = New System.Windows.Forms.Label
            CType(Me.LabelBox.Properties, System.ComponentModel.ISupportInitialize).BeginInit()
            CType(Me.TypeCombo.Properties, System.ComponentModel.ISupportInitialize).BeginInit()
            CType(Me.ServiceParamsGridControl, System.ComponentModel.ISupportInitialize).BeginInit()
            CType(Me.ServiceParamsGridView, System.ComponentModel.ISupportInitialize).BeginInit()
            CType(Me.PriceRI, System.ComponentModel.ISupportInitialize).BeginInit()
            CType(Me.DurationRI, System.ComponentModel.ISupportInitialize).BeginInit()
            CType(Me.DefaultRI, System.ComponentModel.ISupportInitialize).BeginInit()
            CType(Me.CommentMemoEdit.Properties, System.ComponentModel.ISupportInitialize).BeginInit()
            CType(Me.ColorBox.Properties, System.ComponentModel.ISupportInitialize).BeginInit()
            CType(Me.TextColorBox.Properties, System.ComponentModel.ISupportInitialize).BeginInit()
            Me.SuspendLayout()
            '
            'LabelBox
            '
            Me.LabelBox.EditValue = ""
            Me.LabelBox.Location = New System.Drawing.Point(72, 64)
            Me.LabelBox.Name = "LabelBox"
            Me.LabelBox.Size = New System.Drawing.Size(162, 20)
            Me.LabelBox.TabIndex = 1
            '
            'lblLabel
            '
            Me.lblLabel.Location = New System.Drawing.Point(8, 66)
            Me.lblLabel.Name = "lblLabel"
            Me.lblLabel.Size = New System.Drawing.Size(40, 14)
            Me.lblLabel.TabIndex = 0
            Me.lblLabel.Text = "&Label:"
            '
            'lblType
            '
            Me.lblType.Location = New System.Drawing.Point(8, 98)
            Me.lblType.Name = "lblType"
            Me.lblType.Size = New System.Drawing.Size(40, 16)
            Me.lblType.TabIndex = 2
            Me.lblType.Text = "&Type:"
            '
            'TypeCombo
            '
            Me.TypeCombo.EditValue = ""
            Me.TypeCombo.Location = New System.Drawing.Point(72, 96)
            Me.TypeCombo.Name = "TypeCombo"
            '
            'TypeCombo.Properties
            '
            Me.TypeCombo.Properties.Buttons.AddRange(New DevExpress.XtraEditors.Controls.EditorButton() {New DevExpress.XtraEditors.Controls.EditorButton(DevExpress.XtraEditors.Controls.ButtonPredefines.Combo)})
            Me.TypeCombo.Size = New System.Drawing.Size(162, 20)
            Me.TypeCombo.TabIndex = 3
            '
            'ServiceParamsGridControl
            '
            Me.ServiceParamsGridControl.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
                        Or System.Windows.Forms.AnchorStyles.Left), System.Windows.Forms.AnchorStyles)
            '
            'ServiceParamsGridControl.EmbeddedNavigator
            '
            Me.ServiceParamsGridControl.EmbeddedNavigator.Name = ""
            Me.ServiceParamsGridControl.Location = New System.Drawing.Point(8, 224)
            Me.ServiceParamsGridControl.MainView = Me.ServiceParamsGridView
            Me.ServiceParamsGridControl.Name = "ServiceParamsGridControl"
            Me.ServiceParamsGridControl.RepositoryItems.AddRange(New DevExpress.XtraEditors.Repository.RepositoryItem() {Me.PriceRI, Me.DurationRI, Me.DefaultRI})
            Me.ServiceParamsGridControl.Size = New System.Drawing.Size(264, 168)
            Me.ServiceParamsGridControl.TabIndex = 5
            Me.ServiceParamsGridControl.Text = "GridControl1"
            '
            'ServiceParamsGridView
            '
            Me.ServiceParamsGridView.Columns.AddRange(New DevExpress.XtraGrid.Columns.GridColumn() {Me.MasterLevelIDGridColumn, Me.GridColumnPrice, Me.GridColumnDuration, Me.DefaultColumn})
            Me.ServiceParamsGridView.GridControl = Me.ServiceParamsGridControl
            Me.ServiceParamsGridView.Name = "ServiceParamsGridView"
            Me.ServiceParamsGridView.OptionsMenu.EnableColumnMenu = False
            Me.ServiceParamsGridView.OptionsMenu.EnableFooterMenu = False
            Me.ServiceParamsGridView.OptionsMenu.EnableGroupPanelMenu = False
            Me.ServiceParamsGridView.OptionsView.ShowGroupPanel = False
            Me.ServiceParamsGridView.OptionsView.ShowIndicator = False
            Me.ServiceParamsGridView.ShowButtonMode = DevExpress.XtraGrid.Views.Base.ShowButtonModeEnum.ShowForFocusedRow
            '
            'MasterLevelIDGridColumn
            '
            Me.MasterLevelIDGridColumn.Caption = "Level"
            Me.MasterLevelIDGridColumn.DisplayFormat.FormatType = DevExpress.Utils.FormatType.Custom
            Me.MasterLevelIDGridColumn.FieldName = "MasterLevelID"
            Me.MasterLevelIDGridColumn.Name = "MasterLevelIDGridColumn"
            Me.MasterLevelIDGridColumn.Options = CType((((((DevExpress.XtraGrid.Columns.ColumnOptions.CanMoved Or DevExpress.XtraGrid.Columns.ColumnOptions.CanResized) _
                        Or DevExpress.XtraGrid.Columns.ColumnOptions.CanSorted) _
                        Or DevExpress.XtraGrid.Columns.ColumnOptions.ReadOnly) _
                        Or DevExpress.XtraGrid.Columns.ColumnOptions.CanFocused) _
                        Or DevExpress.XtraGrid.Columns.ColumnOptions.NonEditable), DevExpress.XtraGrid.Columns.ColumnOptions)
            Me.MasterLevelIDGridColumn.VisibleIndex = 0
            Me.MasterLevelIDGridColumn.Width = 99
            '
            'GridColumnPrice
            '
            Me.GridColumnPrice.Caption = "Price"
            Me.GridColumnPrice.ColumnEdit = Me.PriceRI
            Me.GridColumnPrice.DisplayFormat.FormatString = "c2"
            Me.GridColumnPrice.DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric
            Me.GridColumnPrice.FieldName = "Price"
            Me.GridColumnPrice.Name = "GridColumnPrice"
            Me.GridColumnPrice.Options = CType((((DevExpress.XtraGrid.Columns.ColumnOptions.CanMoved Or DevExpress.XtraGrid.Columns.ColumnOptions.CanResized) _
                        Or DevExpress.XtraGrid.Columns.ColumnOptions.CanSorted) _
                        Or DevExpress.XtraGrid.Columns.ColumnOptions.CanFocused), DevExpress.XtraGrid.Columns.ColumnOptions)
            Me.GridColumnPrice.VisibleIndex = 1
            Me.GridColumnPrice.Width = 70
            '
            'PriceRI
            '
            Me.PriceRI.AutoHeight = False
            Me.PriceRI.Buttons.AddRange(New DevExpress.XtraEditors.Controls.EditorButton() {New DevExpress.XtraEditors.Controls.EditorButton(DevExpress.XtraEditors.Controls.ButtonPredefines.Combo)})
            Me.PriceRI.DisplayFormat.FormatString = "c2"
            Me.PriceRI.DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric
            Me.PriceRI.EditFormat.FormatString = "f2"
            Me.PriceRI.EditFormat.FormatType = DevExpress.Utils.FormatType.Numeric
            Me.PriceRI.Name = "PriceRI"
            '
            'GridColumnDuration
            '
            Me.GridColumnDuration.Caption = "Duration"
            Me.GridColumnDuration.ColumnEdit = Me.DurationRI
            Me.GridColumnDuration.FieldName = "Duration"
            Me.GridColumnDuration.Name = "GridColumnDuration"
            Me.GridColumnDuration.Options = CType((((DevExpress.XtraGrid.Columns.ColumnOptions.CanMoved Or DevExpress.XtraGrid.Columns.ColumnOptions.CanResized) _
                        Or DevExpress.XtraGrid.Columns.ColumnOptions.CanSorted) _
                        Or DevExpress.XtraGrid.Columns.ColumnOptions.CanFocused), DevExpress.XtraGrid.Columns.ColumnOptions)
            Me.GridColumnDuration.VisibleIndex = 2
            Me.GridColumnDuration.Width = 71
            '
            'DurationRI
            '
            Me.DurationRI.AutoHeight = False
            Me.DurationRI.Buttons.AddRange(New DevExpress.XtraEditors.Controls.EditorButton() {New DevExpress.XtraEditors.Controls.EditorButton})
            Me.DurationRI.Increment = New Decimal(New Integer() {10, 0, 0, 0})
            Me.DurationRI.IsFloatValue = False
            Me.DurationRI.Name = "DurationRI"
            Me.DurationRI.UseCtrlIncrement = True
            '
            'DefaultColumn
            '
            Me.DefaultColumn.ColumnEdit = Me.DefaultRI
            Me.DefaultColumn.Name = "DefaultColumn"
            Me.DefaultColumn.Options = CType(((DevExpress.XtraGrid.Columns.ColumnOptions.ReadOnly Or DevExpress.XtraGrid.Columns.ColumnOptions.FixedWidth) _
                        Or DevExpress.XtraGrid.Columns.ColumnOptions.CanFocused), DevExpress.XtraGrid.Columns.ColumnOptions)
            Me.DefaultColumn.VisibleIndex = 3
            Me.DefaultColumn.Width = 24
            '
            'DefaultRI
            '
            Me.DefaultRI.AutoHeight = False
            Me.DefaultRI.Buttons.AddRange(New DevExpress.XtraEditors.Controls.EditorButton() {New DevExpress.XtraEditors.Controls.EditorButton(DevExpress.XtraEditors.Controls.ButtonPredefines.Ellipsis, "Populate other levels")})
            Me.DefaultRI.Name = "DefaultRI"
            Me.DefaultRI.TextEditStyle = DevExpress.XtraEditors.Controls.TextEditStyles.DisableTextEditor
            '
            'CommentMemoEdit
            '
            Me.CommentMemoEdit.Anchor = CType((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
                        Or System.Windows.Forms.AnchorStyles.Left) _
                        Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
            Me.CommentMemoEdit.EditValue = ""
            Me.CommentMemoEdit.Location = New System.Drawing.Point(288, 64)
            Me.CommentMemoEdit.Name = "CommentMemoEdit"
            Me.CommentMemoEdit.Size = New System.Drawing.Size(224, 328)
            Me.CommentMemoEdit.TabIndex = 10
            '
            'Label1
            '
            Me.Label1.Location = New System.Drawing.Point(8, 208)
            Me.Label1.Name = "Label1"
            Me.Label1.Size = New System.Drawing.Size(88, 16)
            Me.Label1.TabIndex = 9
            Me.Label1.Text = "&Price/Duration:"
            '
            'lblColor
            '
            Me.lblColor.Location = New System.Drawing.Point(8, 130)
            Me.lblColor.Name = "lblColor"
            Me.lblColor.Size = New System.Drawing.Size(40, 16)
            Me.lblColor.TabIndex = 4
            Me.lblColor.Text = "&Color:"
            '
            'ColorBox
            '
            Me.ColorBox.EditValue = System.Drawing.Color.Empty
            Me.ColorBox.Location = New System.Drawing.Point(72, 128)
            Me.ColorBox.Name = "ColorBox"
            '
            'ColorBox.Properties
            '
            Me.ColorBox.Properties.Buttons.AddRange(New DevExpress.XtraEditors.Controls.EditorButton() {New DevExpress.XtraEditors.Controls.EditorButton(DevExpress.XtraEditors.Controls.ButtonPredefines.Combo), New DevExpress.XtraEditors.Controls.EditorButton(DevExpress.XtraEditors.Controls.ButtonPredefines.Ellipsis, "Reset to default color")})
            Me.ColorBox.Size = New System.Drawing.Size(200, 20)
            Me.ColorBox.TabIndex = 5
            '
            'TextColorBox
            '
            Me.TextColorBox.EditValue = System.Drawing.Color.Empty
            Me.TextColorBox.Location = New System.Drawing.Point(72, 160)
            Me.TextColorBox.Name = "TextColorBox"
            '
            'TextColorBox.Properties
            '
            Me.TextColorBox.Properties.Buttons.AddRange(New DevExpress.XtraEditors.Controls.EditorButton() {New DevExpress.XtraEditors.Controls.EditorButton(DevExpress.XtraEditors.Controls.ButtonPredefines.Combo), New DevExpress.XtraEditors.Controls.EditorButton(DevExpress.XtraEditors.Controls.ButtonPredefines.Ellipsis, "Reset to default color")})
            Me.TextColorBox.Size = New System.Drawing.Size(200, 20)
            Me.TextColorBox.TabIndex = 7
            '
            'lblTextColor
            '
            Me.lblTextColor.Location = New System.Drawing.Point(8, 162)
            Me.lblTextColor.Name = "lblTextColor"
            Me.lblTextColor.Size = New System.Drawing.Size(64, 16)
            Me.lblTextColor.TabIndex = 6
            Me.lblTextColor.Text = "&Font Color:"
            '
            'ServiceForm
            '
            Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
            Me.ClientSize = New System.Drawing.Size(522, 406)
            Me.Controls.Add(Me.TextColorBox)
            Me.Controls.Add(Me.lblTextColor)
            Me.Controls.Add(Me.ServiceParamsGridControl)
            Me.Controls.Add(Me.ColorBox)
            Me.Controls.Add(Me.lblColor)
            Me.Controls.Add(Me.Label1)
            Me.Controls.Add(Me.CommentMemoEdit)
            Me.Controls.Add(Me.TypeCombo)
            Me.Controls.Add(Me.lblType)
            Me.Controls.Add(Me.lblLabel)
            Me.Controls.Add(Me.LabelBox)
            Me.Icon = CType(resources.GetObject("$this.Icon"), System.Drawing.Icon)
            Me.Name = "ServiceForm"
            Me.Text = "New Service"
            Me.Controls.SetChildIndex(Me.LabelBox, 0)
            Me.Controls.SetChildIndex(Me.lblLabel, 0)
            Me.Controls.SetChildIndex(Me.lblType, 0)
            Me.Controls.SetChildIndex(Me.TypeCombo, 0)
            Me.Controls.SetChildIndex(Me.CommentMemoEdit, 0)
            Me.Controls.SetChildIndex(Me.Label1, 0)
            Me.Controls.SetChildIndex(Me.lblColor, 0)
            Me.Controls.SetChildIndex(Me.ColorBox, 0)
            Me.Controls.SetChildIndex(Me.ServiceParamsGridControl, 0)
            Me.Controls.SetChildIndex(Me.lblTextColor, 0)
            Me.Controls.SetChildIndex(Me.TextColorBox, 0)
            CType(Me.LabelBox.Properties, System.ComponentModel.ISupportInitialize).EndInit()
            CType(Me.TypeCombo.Properties, System.ComponentModel.ISupportInitialize).EndInit()
            CType(Me.ServiceParamsGridControl, System.ComponentModel.ISupportInitialize).EndInit()
            CType(Me.ServiceParamsGridView, System.ComponentModel.ISupportInitialize).EndInit()
            CType(Me.PriceRI, System.ComponentModel.ISupportInitialize).EndInit()
            CType(Me.DurationRI, System.ComponentModel.ISupportInitialize).EndInit()
            CType(Me.DefaultRI, System.ComponentModel.ISupportInitialize).EndInit()
            CType(Me.CommentMemoEdit.Properties, System.ComponentModel.ISupportInitialize).EndInit()
            CType(Me.ColorBox.Properties, System.ComponentModel.ISupportInitialize).EndInit()
            CType(Me.TextColorBox.Properties, System.ComponentModel.ISupportInitialize).EndInit()
            Me.ResumeLayout(False)

        End Sub

#End Region

#Region " Event Handlers "
        Private mModified As Boolean

        Private Sub ServiceParamsGridView_CellValueChanged(ByVal sender As Object, ByVal e As DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs) Handles ServiceParamsGridView.CellValueChanging
            mModified = True
        End Sub

        Private Sub ServiceParamsGridView_ValidateRow(ByVal sender As Object, ByVal e As DevExpress.XtraGrid.Views.Base.ValidateRowEventArgs) Handles ServiceParamsGridView.ValidateRow
            ValidateServiceParam(e.RowHandle)
        End Sub

        Private Sub ServiceParamsGridView_InvalidRowException(ByVal sender As Object, ByVal e As DevExpress.XtraGrid.Views.Base.InvalidRowExceptionEventArgs) Handles ServiceParamsGridView.InvalidRowException
            e.ExceptionMode = DevExpress.XtraEditors.Controls.ExceptionMode.NoAction
        End Sub


        Private Sub DefaultRI_ButtonClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraEditors.Controls.ButtonPressedEventArgs) Handles DefaultRI.ButtonClick
            If AppMessageBox(Res.PopulateServiceParamsMessage, MsgBoxStyle.Question Or MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
                Dim cur As DataSetData.ServiceParam = ServiceParamsGridView.GetRow(ServiceParamsGridView.FocusedRowHandle)

                mModified = True
                For Each param As DataSetData.ServiceParam In ServiceParamsGridView.DataSource
                    If Not param Is cur Then
                        param.Duration = cur.Duration
                        param.Price = cur.Price
                    End If
                Next
                ServiceParamsGridView.CloseEditor()
                ServiceParamsGridControl.RefreshDataSource()
            End If
        End Sub

        Private Sub ColorBox_ButtonClick(ByVal sender As Object, ByVal e As DevExpress.XtraEditors.Controls.ButtonPressedEventArgs) Handles ColorBox.ButtonClick
            ColorBox.EditValue = DBNull.Value
        End Sub

        Private Sub TextColorBox_ButtonClick(ByVal sender As Object, ByVal e As DevExpress.XtraEditors.Controls.ButtonPressedEventArgs) Handles TextColorBox.ButtonClick
            TextColorBox.EditValue = DBNull.Value
        End Sub
#End Region

#Region " Data "
        Public Shadows Property DataSource() As DataSetData.Service
            Get
                Return CType(MyBase.DataSource, DataSetData.Service)
            End Get
            Set(ByVal Value As DataSetData.Service)
                MyBase.DataSource = Value
            End Set
        End Property

        Protected Overrides Sub DoBinding()
            mModified = False

            ' Load Types
            TypeCombo.Properties.Items.Clear()
            Dim dv As DataView = New DataView(AppDataLayer.Services)
            dv.Sort = "Type ASC"

            Dim type As String = String.Empty
            For Each row As DataRowView In dv
                Dim stype As String = CType(row.Row, DataSetData.Service).Type
                If (stype <> String.Empty) And (stype <> type) Then
                    TypeCombo.Properties.Items.Add(stype)
                    type = stype
                End If
            Next


            BindWithCapitalization(LabelBox, "Text", DataSource, "Label")
            'LabelBox.DataBindings.Add("Text", DataSource, "Label")

            BindWithCapitalization(TypeCombo, "Text", DataSource, "Type")
            'TypeCombo.DataBindings.Add("Text", DataSource, "Type")

            CommentMemoEdit.DataBindings.Add("Text", DataSource, "Comment")

            'Colors 
            ColorBox.DataBindings.Add("EditValue", Me, "ServiceColor")
            TextColorBox.DataBindings.Add("EditValue", Me, "ServiceTextColor")

            'Load Service Parameters
            ServiceParamsGridControl.DataSource = Helpers.ServiceHelper.GetServiceParams(DataSource)

            ' Apply Security settings
            LabelBox.Properties.ReadOnly = Not (Security.CanCreateService())
            TypeCombo.Properties.ReadOnly = Not (Security.CanCreateService())
            CommentMemoEdit.Properties.ReadOnly = Not (Security.CanCreateService())
            ServiceParamsGridView.OptionsBehavior.Editable = Security.CanCreateService()
        End Sub

        Public Property ServiceColor() As Object
            Get
                If DataSource.Color = 0 Then
                    Return DBNull.Value
                Else
                    Return Color.FromArgb(DataSource.Color)
                End If
            End Get
            Set(ByVal Value As Object)
                If Value Is DBNull.Value Then
                    DataSource.Color = 0
                Else
                    DataSource.Color = CType(Value, Color).ToArgb
                End If
            End Set
        End Property

        Public Property ServiceTextColor() As Object
            Get
                If DataSource.TextColor = 0 Then
                    Return DBNull.Value
                Else
                    Return Color.FromArgb(DataSource.TextColor)
                End If
            End Get
            Set(ByVal Value As Object)
                If Value Is DBNull.Value Then
                    DataSource.TextColor = 0
                Else
                    DataSource.TextColor = CType(Value, Color).ToArgb
                End If
            End Set
        End Property



        Protected Overrides Function IsDataUpdated() As Boolean
            If DataSource.RowState <> DataRowState.Unchanged Then
                Return True
            End If

            If mModified Then
                Return True
            End If
            Return False
        End Function


        Protected Overrides Sub RejectChanges()
            DataSource.RejectChanges()
            If mModified Then
                For Each param As DataSetData.ServiceParam In ServiceParamsGridView.DataSource
                    If param.RowState <> DataRowState.Unchanged Then
                        param.RejectChanges()
                    End If
                Next
            End If
        End Sub

        Public Overrides ReadOnly Property DocumentTitle() As String
            Get
                Return DataSource.Label()
            End Get
        End Property

        Public Overrides ReadOnly Property DocumentType() As String
            Get
                Return Res.TypeServiceDocument
            End Get
        End Property
#End Region

#Region " Life-Cycle Routines "
        Protected Overrides Sub OnDelete()
            Helpers.ServiceHelper.DeleteService(DataSource, Me)
        End Sub

        Protected Overrides Sub DoSaving()
            Dim c As Control = ActiveControl
            LabelBox.Focus()
            CommentMemoEdit.Focus()
            LabelBox.Focus()
            If Not c Is Nothing Then c.Focus()

            Helpers.ServiceHelper.UpdateService(DataSource, Me)
        End Sub

        Protected Overrides Function CreateNewRow() As DataRow
            Return Helpers.ServiceHelper.CreateService()
        End Function

        Protected Overrides Sub UpdateSecurityEditCommands()
            If DeleteBarItem.Enabled Then
                DeleteBarItem.Enabled = Security.CanDeleteService

            End If
            SaveBarItem.Enabled = Security.CanCreateService
            SaveCloseBarItem.Enabled = Security.CanCreateService
            SaveNewBarItem.Enabled = Security.CanCreateService
        End Sub

#End Region

#Region " Validation "

        Private Function ValidateServiceParam(ByVal RowHandle As Integer) As Boolean
            ServiceParamsGridView.FocusedRowHandle = RowHandle

            Dim Result As Boolean = True

            'REQUESTED TO REMOVE: TTP #324
            'If CDbl(ServiceParamsGridView.GetRowCellValue(RowHandle, GridColumnPrice)) <= 0 Then
            '    Result = False
            '    ServiceParamsGridView.SetColumnError(GridColumnPrice, Res.ErrorZeroCostService)
            'End If

            If CLng(ServiceParamsGridView.GetRowCellValue(RowHandle, GridColumnDuration)) < 0 Then
                Result = False
                ServiceParamsGridView.SetColumnError(GridColumnDuration, Res.ErrorZeroDurationService)
            End If

            Return Result
        End Function


        Private Function ValidateServiceParams() As Boolean
            For i As Integer = 0 To ServiceParamsGridView.RowCount - 1
                If Not ValidateServiceParam(i) Then
                    Return False
                End If
            Next
            Return True
        End Function


        Public Overrides Function DataValidate() As Boolean
            If Not MyBase.DataValidate Then Return False

            ServiceParamsGridView.CloseEditor()

            Dim Res As Boolean = True

            If Not ValidateServiceParams() Then
                Res = False
            End If

            Return Res
        End Function
#End Region

    End Class

#Region " MasterLevelFormatter "
    Public Class MasterLevelFormatter
        Implements IFormatProvider, ICustomFormatter

        Public Function GetFormat(ByVal type As System.Type) As Object Implements IFormatProvider.GetFormat
            Return Me
        End Function

        Public Function Format(ByVal formatString As String, ByVal arg As Object, ByVal formatProvider As IFormatProvider) As String Implements ICustomFormatter.Format
            If arg.GetType() Is GetType(String) Then
                Return arg
            ElseIf arg.GetType() Is GetType(Integer) Then
                Return AppDataLayer.MasterLevels.FindByMasterLevelID(Long.Parse(CType(arg, String))).LevelName
            ElseIf arg.GetType() Is GetType(DataSetData.MasterLevel) Then
                Return CType(arg, DataSetData.MasterLevel).LevelName
            Else
                Return String.Empty
            End If
        End Function

    End Class
#End Region

End Namespace